﻿using System;
using ValidationEngine.Commons;
using ValidationEngine.Commons.Enums;
using ValidationEngine.Commons.Interfaces;
using ValidationEngine.Tools;

namespace ValidationEngine.ValidationRules
{
    public class ContainsSubstringRule : BaseValidationRule
    {
        #region Members
        private string _subString;
        #endregion

        protected override IValidationInfo ApplyRule(IValidationContext ctx)
        {
            DefResult = ModelHelper.GetPropertyValue(ctx).ToString().Contains(_subString) ? _defErrorMsg : null;
            return Result;
        }

        #region Ctor
        public ContainsSubstringRule(
         string subString,
         string defErrorText = null,
         RuleApplingContext ruleApplingContext = RuleApplingContext.Always,
         ValidationErrorType errorType = ValidationErrorType.Error)
        {
            this._subString = subString;
            this.ApplingContext = ruleApplingContext;
            this._defErrorType = errorType;
            this._defErrorMsg = defErrorText ?? String.Format("Value should not contain: '{0}'", subString);
        }
        #endregion
    }
}
